package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"good/good"
	"good/internal/svc"
	"good/model/mysql"
	"good/model/redis"
)

type GoodCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodCreateLogic {
	return &GoodCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodCreateLogic) GoodCreate(in *good.GoodCreateRequest) (*good.GoodCreateResponse, error) {
	// todo: add your logic here and delete this line
	m := mysql.Good{
		GoodTitle:  in.GoodTitle,
		GoodPrice:  in.GoodPrice,
		GoodStock:  in.GoodStock,
		GoodCover:  in.GoodCover,
		GoodStatus: in.GoodStatus,
	}
	err := m.GoodCreate()
	if err != nil {
		return nil, err
	}
	logx.Error(err)
	redis.SyncShopStock(int(m.ID), int(m.GoodStock)) //向虚拟库存中添加商品
	return &good.GoodCreateResponse{Id: int64(m.ID)}, nil
}
